import { obj_empty, loc } from 'sui';
import { get_f } from '../cmd';

const cl_opt = (opt, cl, lcl, ucl) => {
    const ret = {};

    if (opt.s_cl) {
        let tmp = get_f(opt, "cl");
        if (isNaN(tmp)) {
            ret["cl"] = parseFloat(cl);
        }
        else {
            ret["cl"] = tmp;
        }
    }

    if (opt.s_lcl) {
        let tmp = get_f(opt, "lcl");
        if (isNaN(tmp)) {
            ret["lcl"] = parseFloat(lcl);
        }
        else {
            ret["lcl"] = tmp;
        }
    }

    if (opt.s_ucl) {
        let tmp = get_f(opt, "ucl");
        if (isNaN(tmp)) {
            ret["ucl"] = parseFloat(ucl);
        }
        else {
            ret["ucl"] = tmp;
        }
    }

    return ret;
};

const getData = (main) => {
    if (obj_empty(main) || obj_empty(main.g)) {
        return [null, null];
    }

    const num = main.row;
    let col = [
        {name: loc("db_019"), key: 'index', width: 70}
    ];

    for (let i = 0; i < num; i++) {
        col.push({name: `${loc("product_44")}${i + 1}`, key: `d${i}`, width: 100});
    }

    let row = [];

    main.g.forEach((d, i) => {
        const tmp = {index: i + 1};

        for (let i = 0; i < num; i++) {
            let v = parseFloat(d[i]);
            if (isNaN(v)) {
                v = "";
            }

            tmp[`d${i}`] = v;
        }

        row.push(tmp);
    });

    return [col, row];
};

const get_adjust = (maindata, main, lopt, opt) => {
    let adjust = {
        "pt_1": [],
        "pt_7": [],
    };

    const std = 3 * parseFloat(main.n.std);

    let updown = [];
    let tp = "none";

    for (let i = 0; i < maindata.length; i++) {
        let v = [i, maindata[i]];

        const d = maindata[i] - lopt.cl;
        if (opt.pt_1) {
            if (Math.abs(d) >= std) {
                adjust.pt_1.push(v);
            }
        }

        if (opt.pt_7) {
            const len = updown.length;
            if (d > 0) {
                if (tp !== "up") {
                    if (len < 7) {
                        updown = [];
                    }
                    else {
                        adjust.pt_7 = adjust.pt_7.concat(updown);
                        updown = [];
                    }
                }

                tp = "up";
            }
            else if (d < 0) {
                if (tp !== "down") {
                    if (len < 7) {
                        updown = [];
                    }
                    else {
                        adjust.pt_7 = adjust.pt_7.concat(updown);
                        updown = [];
                    }
                }

                tp = "down";
            }

            updown.push(v);
        }// if
    }// for

    if (updown.length >= 7) {
        adjust.pt_7 = adjust.pt_7.concat(updown);
    }

    return adjust;
};

const filterE = (k, main, opt) => {
    if (obj_empty(main) || obj_empty(main[k]) || !opt) {
        return null;
    }

    const { maindata, cl, lcl, ucl } = main[k];

    const lopt = cl_opt(opt, cl, lcl, ucl);

    const line = [];
    let min = maindata[0];
    let max = maindata[0];

    maindata.forEach((d, i) => {
        line.push([i, d]);

        min = Math.min(min, d);
        max = Math.max(max, d);
    });

    min = Math.min(min, lopt.lcl);
    max = Math.max(max, lopt.ucl);

    const end = line.length - 1;

    return {
        line: [line],
        min: min,
        max: max,
        cl: [[0, lopt.cl], [end, lopt.cl]],
        ucl: [[0, lopt.ucl], [end, lopt.ucl]],
        lcl: [[0, lopt.lcl], [end, lopt.lcl]],
        adj: get_adjust(maindata, main, lopt, opt),
    }
};

export {filterE, getData};
